package com.hkedou.yuanhuo.shiro;

import org.apache.shiro.web.filter.authc.FormAuthenticationFilter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

public class MyFormAuthenticationFilter extends FormAuthenticationFilter {

    final static String DEBUG_PREFIX="-----------------------";

    private static Logger logger= LoggerFactory.getLogger("com.hkedou.shiro");


    @Override
    public void setLoginUrl(String loginUrl) {
        for (String key:this.appliedPaths.keySet()){
            logger.debug(DEBUG_PREFIX+"key="+key);
        }
        if(this.getLoginUrl()!=null){
            this.appliedPaths.get(loginUrl);
        }
        super.setLoginUrl(loginUrl);
    }

    @Override
    protected boolean isAccessAllowed(ServletRequest request, ServletResponse response, Object mappedValue) {
        String requestURI = this.getPathWithinApplication(request);
        logger.debug(DEBUG_PREFIX+"MyFormAuthenticationFilter.isAccessAllowed.requestURI="+requestURI);
        boolean accessAllowed = super.isAccessAllowed(request, response, mappedValue);
        logger.debug(DEBUG_PREFIX+"MyFormAuthenticationFilter.isAccessAllowed.accessAllowed="+accessAllowed);
        return accessAllowed;
    }

    @Override
    protected boolean onAccessDenied(ServletRequest request, ServletResponse response, Object mappedValue) throws Exception {
        String requestURI = this.getPathWithinApplication(request);
        logger.debug(DEBUG_PREFIX+"MyFormAuthenticationFilter.onAccessDenied.requestURI="+requestURI);

        boolean accessDenied = super.onAccessDenied(request, response, mappedValue);
        logger.debug(DEBUG_PREFIX+"MyFormAuthenticationFilter.onAccessDenied.accessDenied="+accessDenied);
        return accessDenied;
    }
}

